package com.zcoj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcoj.core.model.PageData;
import com.zcoj.core.model.PageWrap;
import com.zcoj.mapper.JudgeMapper;
import com.zcoj.mapper.ProblemMapper;
import com.zcoj.pojo.Dto.*;
import com.zcoj.pojo.Vo.ProblemPageVO;
import com.zcoj.service.ContestProblemService;
import com.zcoj.service.GroupProblemService;
import com.zcoj.service.ProblemVoService;
import com.zcoj.service.TrainingProblemService;
import com.zcoj.util.WrapperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author chenbin
 * @version 1.0
 * @description: TODO
 * @date 2023/5/8 17:26
 */
@Service
public class ProblemVoServiceImpl implements ProblemVoService {


    @Autowired
    private JudgeMapper judgeMapper;


    @Autowired
    private ProblemMapper problemMapper;



    @Autowired
    private GroupProblemService groupProblemService;

    @Autowired
    private ContestProblemService contestProblemService;

    @Autowired
    private TrainingProblemService trainingProblemService;


    /**
     * @method: [findCompentPageVO]
     * @param: [pageWrap]
     * @returns: com.zcoj.core.model.PageData<com.zcoj.pojo.Vo.ProblemPageVO>
     * @description:   前台组件无条件查询
     * @author: chen.bin
     * @version: 1.0.0
     * @date 2023/4/21 22:19
     **/
    @Override
    public PageData<ProblemPageVO> findComponentPageVO(PageWrap<Problem> pageWrap) {
        IPage<Problem> page=new Page<>(pageWrap.getPage(),pageWrap.getCapacity());

        QueryWrapper<Problem> queryWrapper=new QueryWrapper<>();
        Problem problem = WrapperUtil.blankToNull(pageWrap.getModel());


        if(Objects.nonNull(problem.getGid())&&problem.getGid()!=0){
            //queryWrapper.eq("gid",problem.getGid());
            GroupProblem groupProblem = new GroupProblem();
            groupProblem.setGid(problem.getGid());
            List<GroupProblem> list = groupProblemService.findList(groupProblem);

            if(list.size()!=0){
                queryWrapper.nested(i -> {
                    for (GroupProblem gp : list) {
                        i.or(j -> j.eq("id", gp.getPid()));
                    }
                });
            }else {
                queryWrapper.eq("id",0);
            }


        }else if(Objects.nonNull(problem.getTid())&&problem.getTid()!=0){
            //queryWrapper.eq("tid",problem.getTid());
            TrainingProblem trainingProblem=new TrainingProblem();
            trainingProblem.setTid(problem.getTid());
            List<TrainingProblem> trainingProblemList = trainingProblemService.findList(trainingProblem);

            if(trainingProblemList.size()!=0){
                queryWrapper.nested(i->{
                    for (TrainingProblem tp : trainingProblemList) {
                        i.or(j->j.eq("id",tp.getPid()));
                    }
                });
            }else {
                queryWrapper.eq("id",0);
            }

        }else if(Objects.nonNull(problem.getCid())&&problem.getCid()!=0){
            //queryWrapper.eq("cid",problem.getCid());
            ContestProblem contestProblem = new ContestProblem();
            contestProblem.setCid(problem.getCid());
            List<ContestProblem> list = contestProblemService.findList(contestProblem);
            if(list.size()!=0){
                queryWrapper.nested(i->{
                    for (ContestProblem cp : list) {
                        i.or(j->j.eq("id",cp.getPid()));
                    }
                });
            }else {
                queryWrapper.eq("id",0);
            }

        }
        queryWrapper.select("id","tilte","source","tag");
        IPage<Problem> iPage = problemMapper.selectPage(page, queryWrapper);
        List<ProblemPageVO> problemPageVOS=new ArrayList<>();


        //查询SubmitNumber AccessNumber
        for (Problem p : iPage.getRecords()) {
            ProblemPageVO problemPageVO = new ProblemPageVO();
            problemPageVO.setId(p.getId());
            problemPageVO.setTilte(p.getTilte());
            problemPageVO.setSource(p.getSource()==null?0:p.getSource());
            problemPageVO.setTag(p.getTag()==null?0:p.getTag());
            QueryWrapper<Judge> judgeQueryWrapper=new QueryWrapper<>();
            judgeQueryWrapper.eq("pid",p.getId());
            problemPageVO.setSubmitNumber(judgeMapper.selectCount(judgeQueryWrapper));
            judgeQueryWrapper.eq("status",0);
            problemPageVO.setAccessNumber(judgeMapper.selectCount(judgeQueryWrapper));
            problemPageVOS.add(problemPageVO);
        }


        //将Probelem类映射到Vo类上
        IPage<ProblemPageVO> problemPageVOIPage=new Page<>();
        problemPageVOIPage.setTotal(iPage.getTotal());
        problemPageVOIPage.setCurrent(iPage.getCurrent());
        problemPageVOIPage.setSize(iPage.getSize());
        problemPageVOIPage.setRecords(problemPageVOS);
        return PageData.from(problemPageVOIPage);
    }

}
